package bud.vo;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.commons.codec.binary.Base64;
import org.hibernate.validator.NotEmpty;
import org.springframework.web.multipart.MultipartFile;

import lombok.Data;

@Data
public class ShopDTO {
	private Integer sh_no;
	private String sh_name;
	private String sh_phone;
	private String sh_add;
	private String sh_message;
	private MultipartFile sh_photo;
	private byte[] sh_photoStream;
	
	private double sh_latitude;
	private double sh_longitude;
	private String sh_pass;
	
	public void setSh_photo(MultipartFile sh_photo) throws IOException{
		this.sh_photo = sh_photo;
		if(sh_photo.isEmpty()) {
			return;
		}
		
		BufferedImage originalImg = ImageIO.read(sh_photo.getInputStream());
		int maxLength = 500;
		int destWidth = 0;
		int destHeight = 0;
		if (originalImg.getWidth() > maxLength || originalImg.getHeight() > maxLength){	
			if (originalImg.getWidth() > originalImg.getHeight()){
				destWidth = maxLength;
				destHeight = (int)(originalImg.getHeight()/(double)originalImg.getWidth() * maxLength);
			} else {
				destWidth = (int)(originalImg.getWidth()/(double)originalImg.getHeight() * maxLength);
				destHeight = maxLength;
			}
		} else {
			destWidth = originalImg.getWidth();
			destHeight = originalImg.getHeight();
		}
		
		BufferedImage resizeImg = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = resizeImg.createGraphics();
		g.drawImage(originalImg, 0, 0, destWidth, destHeight, null);
		g.dispose();

		ByteArrayOutputStream os = new ByteArrayOutputStream();
		ImageIO.write(resizeImg, "jpg", os);
		sh_photoStream = os.toByteArray();
//		sh_photoStream = Base64.encodeBase64(sh_photo.getBytes()) ;
	}
	
	public String getSh_photoStr(){
		return sh_photoStream==null?null:Base64.encodeBase64String(sh_photoStream);
	}
}
